/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.charlie.sessions;

import com.charlie.beans.ShipmentOrder;
import com.charlie.beans.SoHistory;
import com.charlie.beans.StatusDict;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Вова
 */
@Stateless
public class SoHistoryFacade extends AbstractFacade<SoHistory> implements SoHistoryFacadeLocal {

    @PersistenceContext(unitName = "CharlieShop-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public SoHistoryFacade() {
        super(SoHistory.class);
    }

    @Override
    public SoHistory findItem(ShipmentOrder s, StatusDict status) {
        SoHistory r = null;
        try {
            Query q = em.createQuery("Select p FROM SoHistory p where p.soId=?1");// WHERE p.soId in (Select orderSt.soId from SoHistory orderSt where orderSt.statusDictId =:stID) ", ShipmentOrder.class);
            q.setParameter(1, s);

            r = (SoHistory)q.getSingleResult();
            System.err.println( "IN EJB HISTORY ITEM" + r.getStatusDictId());

            return r;

        } catch (NoResultException ex ) {
            return r;
        }
    }

    @Override
    public SoHistory update(SoHistory h) {
        
        try {
            System.err.println("OUR COOMMENTS IS "+ h.getCommentary());
            System.err.println("before merger");
            em.merge(h);
            SoHistory temo = em.find(SoHistory.class, h.getSoHistoryId());
           System.err.println("temo " + temo.getCommentary());

        } catch (NoResultException ex) {
        }
        return h;
    }

    @Override
    public void createNewItem(SoHistory soHistory) {
        SoHistory newItem =null;
       try {
           newItem=new SoHistory(); 
           newItem.setChangeDate(soHistory.getChangeDate());
       
        newItem.setCommentary(soHistory.getCommentary());
        newItem.setSoId(soHistory.getSoId());
        newItem.setUserId(soHistory.getUserId());
       
        em.persist(newItem);
       }catch (NoResultException e )
       {
           
       }               
       }
}
